package com.jelly.web.share.vcode.controller;

import com.jelly.common.constant.CodeConstant;
import com.jelly.web.share.vcode.domain.Captcha;
import com.jelly.web.share.vcode.domain.GifCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @创建人 ql
 * @创建时间 2018-08-14 13:29
 * @项目名称 jelly
 * @功能描述: 获取验证码（Gif版本）
 **/
@Controller
public class GifCodeController {
    /**
     * 获取验证码（Gif版本）
     * @param response
     */
    @GetMapping("/getGifCode")
    public void getGifCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession();
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/gif");
            response.setCharacterEncoding("utf-8");
            /**
             * gif格式动画验证码
             * 宽，高，位数。
             */
            Captcha captcha = new GifCaptcha(146, 33, 4);
            //输出
            captcha.out(response.getOutputStream());
            //存入Session
            session.setAttribute(CodeConstant.GIFCODESESSION+session.getId(), captcha.text().toLowerCase());

        } catch (Exception e) {
            System.err.println("获取验证码异常：" + e.getMessage());
        }
    }
    }
